package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.ButtonMapper;
import com.zhijian.medical.entity.po.Button;
import com.zhijian.medical.entity.vo.request.ButtonSearchVo;
import com.zhijian.medical.entity.vo.response.ButtonVo;
import com.zhijian.medical.service.ButtonService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 按钮业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-23 15:36:11
 */
@Slf4j
@Service
public class ButtonServiceImpl implements ButtonService {

    @Autowired
    private ButtonMapper mapper;

    @Override
    public Button selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ButtonVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(Button button) {
        Date now = new Date();
        button.setCreateTime(now);
        button.setUpdateTime(now);
        return mapper.insert(button) > 0 ? button.getId() : 0L;
    }

    @Override
    public boolean update(Button button) {
        Date now = new Date();
        button.setUpdateTime(now);
        return mapper.updateByPrimaryKey(button) != 0;
    }

    @Override
    public Integer count(ButtonSearchVo buttonSearchVo) {
        return mapper.count(buttonSearchVo);
    }

    @Override
    public List<ButtonVo> selectList(ButtonSearchVo buttonSearchVo) {
        return mapper.selectList(buttonSearchVo);
    }

    @Override
    public Page<ButtonVo> selectPage(ButtonSearchVo buttonSearchVo) {
        //先直接查总数
        int count = count(buttonSearchVo);
        //分页
        Page<ButtonVo> page = new Page<>(true, count, buttonSearchVo.getPageNum(), buttonSearchVo.getPageSize());
        buttonSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ButtonVo> responseVos = this.selectList(buttonSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public List<ButtonVo> selectListByMenuId(Long menuId) {
        return mapper.selectListByMenuId(menuId);
    }

}